<?php
    class Solution{
        /**
         * 830. 较大分组的位置
         * @param String $s
         * @return Integer[][]
         */
        function largeGroupPositions830($s) {
            $now = '';
            $map = [];
            $start = 0;
            for($i = 0;$i < strlen($s);$i++){
                if($s[$i] != $now){
                    print_r($s[$i]);
                    $now = $s[$i];
                    if($i - $start >= 3){
                        $map[] = [$start,$i - 1];
                    }
                    $start = $i;
                }
            }
            if($i - $start >= 3){
                $map[] = [$start,$i - 1];
            }

            return $map;
        }

        /**
         * 832. 翻转图像
         * @param Integer[][] $image
         * @return Integer[][]
         */
        function flipAndInvertImage832($image) {
            $n = count($image);
            $check = ($n % 2) == 0;
            foreach($image as &$item){
                foreach($item as  $key => &$v){
                    if($key >= (($n-1)/2)){
                        if(!$check){
                            $v = 1 - $v;
                        }
                        break;
                    }
                    if($v == $item[$n-1-$key]){
                        $v = 1 - $v;
                        $item[$n-1-$key] = 1 - $item[$n-1-$key];
                    }
                }
            }

            return $image;
        }

        /**
         * 836. 矩形重叠
         * @param Integer[] $rec1
         * @param Integer[] $rec2
         * @return Boolean
         */
        function isRectangleOverlap836($rec1, $rec2){
            $x = $rec1[2] > $rec2[0] && $rec1[0] < $rec2[2];
            $y = $rec1[3] > $rec2[1] && $rec1[1] < $rec2[3];

            return $x && $y;
        }

        /**
         * 844. 比较含退格的字符串
         * @param String $s
         * @param String $t
         * @return Boolean
         */
        function backspaceCompare844($s, $t) {
            $stackS = [];
            $stackT = [];
            for ($i = 0; $i < strlen($s); $i++) {
                if ($s[$i] != '#') {
                    $stackS[] = $s[$i];
                } else {
                    if (!empty($stackS)) {
                        array_pop($stackS);
                    }
                }
            }
            for ($i = 0; $i < strlen($t); $i++) {
                if ($t[$i] != '#') {
                    $stackT[] = $t[$i];
                } else {
                    if (!empty($stackT)) {
                        array_pop($stackT);
                    }
                }
            }

            return $stackS == $stackT;
        }


        /**
         * 859. 亲密字符串
         * @param String $s
         * @param String $goal
         * @return Boolean
         */
        function buddyStrings859($s, $goal) {
            if(strlen($s) != strlen($goal)){
                return false;
            }
            $no = [];
            for($i = 0;$i < strlen($s);$i++){
                if($s[$i] == $goal[$i]){
                    continue;
                }else{
                    $no[] = $s[$i];
                    $no[] = $goal[$i];
                    if(count($no) > 4){
                        return false;
                    }
                }
            }
            if(empty($no)){
                print_r(array_sum(count_chars($s, 1)));
                if(array_sum(count_chars($s, 1)) < strlen($goal)){
                    return true;
                }else{
                    return false;
                }
            }

            return ($no[0] == $no[3] && $no[1] == $no[2]);
        }

        /**
         * 860. 柠檬水找零
         * @param Integer[] $bills
         * @return Boolean
         */
        function lemonadeChange($bills) {
            $five = 0;
            $ten  = 0;
            foreach($bills as $bill){
                switch ($bill) {
                    case 5:
                        $five ++;
                        break;
                    case 10:
                        if($five <= 0){
                            return false;
                        }else{
                            $five --;
                            $ten ++;
                        }
                        break;
                    case 20:
                        if($five > 0 && $ten > 0){
                            $five --;
                            $ten --;
                        }elseif($five >= 3){
                            $five -= 3;
                        }else{
                            return false;
                        }
                        break;
                }
            }
            return true;
        }

        /**
         * 867. 转置矩阵
         * @param Integer[][] $matrix
         * @return Integer[][]
         */
        function transpose($matrix) {
            $new = [];
            foreach($matrix as $rows => $row){
                foreach($row as $columns => $item){
                    $new[$columns][$rows] = $item;
                }
            }

            return $new;
        }

        /**
         * 868. 二进制间距
         * @param Integer $n
         * @return Integer
         */
        function binaryGap($n) {
            $bin = decbin($n);
            $max = 0;
            for($i = 0;$i < strlen($bin);$i++){
                if($bin[$i] == 1){
                    if(isset($now)){
                        $max = max($max,($i - $now));
                    }
                    $now = $i;
                }
            }

            return $max;
        }

        /**
         * 883. 三维形体投影面积
         * @param Integer[][] $grid
         * @return Integer
         */
        function projectionArea883($grid) {
            $zMax = 0;
            $xMax = [];
            $yMax = [];
            foreach($grid as $row){
                $xMax[] = max($row);
                foreach($row as $key => $col){
                    $yMax[$key] = isset($yMax[$key]) ? max($yMax[$key],$col) : $col;
                    if($col > 0){
                        $zMax++;
                    }
                }
            }

            return array_sum($xMax) + array_sum($yMax) + $zMax;
        }

        /**
         * 884. 两句话中的不常见单词
         * @param String $s1
         * @param String $s2
         * @return String[]
         */
        function uncommonFromSentences884($s1, $s2) {
            $s1 = explode(' ', $s1);
            $s2 = explode(' ', $s2);
            $count1 = array_count_values($s1);
            $count2 = array_count_values($s2);

            $totalCounts = $count1;
            foreach ($count2 as $word => $count) {
                if (isset($totalCounts[$word])) {
                    $totalCounts[$word] += $count;
                } else {
                    $totalCounts[$word] = $count;
                }
            }

            $uniqueWords = array_keys(array_filter($totalCounts, function($v) {
                return $v === 1;
            }));

            return $uniqueWords;
        }

        }

    $solution = new Solution();
    $s1 = "this apple is sweet";
    $s2 = "this apple is sour";
    print_r($solution->uncommonFromSentences884($s1, $s2));
